package controller

import (
	"ollama-go/dto"
	"ollama-go/query"
	"ollama-go/service"
	"ollama-go/toolkit"

	"github.com/androidsr/sc-go/model"
	"github.com/androidsr/sc-go/sgin"
	"github.com/gin-gonic/gin"
)

func init() {
	sgin.AddRouter(OllamaCollectionController{
		ollamaCollectionService: service.NewOllamaCollectionService(),
	})
}

type OllamaCollectionController struct {
	ollamaCollectionService *service.OllamaCollectionService
	ollamaEmbeddingService  *service.OllamaEmbeddingService
}

// 查询
// @Router [get] [/ollamacollection/:id]
func (m OllamaCollectionController) Get(c *gin.Context) any {
	id := c.Param("id")
	if id == "" {
		return model.NewFailDefault("ID不能为空")
	}
	return m.ollamaCollectionService.Get(id)
}

// 增加
// @Router [post] [/ollamacollection]
func (m OllamaCollectionController) Save(c *gin.Context, dto *dto.OllamaCollectionDTO) any {
	dto.Uuid = ""
	dto.TenantId = c.GetString(USER)
	metadata := make(map[string]any, 0)
	metadata["tenantId"] = dto.TenantId
	metadata["isOpen"] = dto.IsOpen
	dto.TenantId = c.GetString(USER)
	return toolkit.PG.AddStore(c.Request.Context(), dto.Name, metadata)
}

// 修改
// @Router [put] [/ollamacollection/edit]
func (m OllamaCollectionController) UpdateById(c *gin.Context, dto *dto.OllamaCollectionDTO) any {
	if dto.Uuid == "" {
		return model.NewFailDefault("ID不能为空")
	}
	dto.TenantId = c.GetString(USER)
	return m.ollamaCollectionService.UpdateById(dto)

}

// 删除
// @Router [delete] [/ollamacollection/:id]
func (m OllamaCollectionController) DeleteById(c *gin.Context) any {
	id := c.Param("id")
	if id == "" {
		return model.NewFailDefault("ID不能为空")
	}
	return m.ollamaCollectionService.DeleteById(id)
}

// 查询列表
// @Router [post] [/ollamacollection/page]
func (m OllamaCollectionController) QueryPage(c *gin.Context, query *query.OllamaCollectionQueryDTO) any {
	query.TenantId = c.GetString(USER)
	return m.ollamaCollectionService.QueryPage(query)
}

// 分页下拉
// @Router [post] [/ollamacollection/list]
func (m OllamaCollectionController) QuerySelect(c *gin.Context, query *model.SelectQueryDTO) any {
	if query.Vars == nil {
		query.Vars = make(map[string]interface{})
	}
	query.Vars["tenantId"] = c.GetString(USER)
	return m.ollamaCollectionService.QueryList(query)
}
